import {
	msg
} from './util.js';



//let BASE_URL = 'http://www.qzyhjs.com/index';
let BASE_URL = 'http://192.168.88.237:8787/api/patient';
// let BASE_URL = 'https://qzy.dianteng.cc/index';
// let BASE_URL = 'https://www.qunzhongyuan.com/index';
// let BASE_URL = '/api';  


// 1.导入
import store from '@/store';

const request = (options = {}) => {

	// 判断是否有访问令牌
	const openid = store.state.openid;
	if (openid) {
		// oauth2协议
		options.header = {
			'Authorization': `${openid}`,
			'content-type':'application/x-www-form-urlencoded'
		};
	}

	// 短信验证码也要放行
	const whileList = ['/system/sms/code', '/auth/login', '/auth/login/provider', '/auth/user/mobile'];

	// resolve 正常响应，reject异常响应
	return new Promise((resolve, reject) => {
		if (options.isLogin && !openid) {
			//跳转到登录页
			return uni.navigateTo({
				url: '/pages/auth/login'
			});
			
		}


		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			timeout: 8000, // 8秒超时时间，单位ms
			header: options.header || {},
			success: (res) => {
				// console.log('res', res.data)
				resolve(res.data);
			},
			fail: (err) => {
				// console.log('err', err)
				msg('请求接1口失败');
				reject(err);
			}
		});
	});
};

// 导出
export default request;
